package org.demo.szml.perssionservice.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.demo.szml.api.dto.PermissionBindingReqDTO;
import org.demo.szml.api.dto.PermissionBindingResDTO;
import org.demo.szml.api.exception.ApiInvokeBusinessException;
import org.demo.szml.perssionservice.config.PermissionRoleConfiguration;
import org.demo.szml.perssionservice.repository.IPermissionRepository;
import org.demo.szml.perssionservice.repository.mapper.RolesMapper;
import org.demo.szml.perssionservice.repository.mapper.UserRolesMapper;
import org.demo.szml.perssionservice.repository.po.UserRolesPO;
import org.demo.szml.perssionservice.service.IPermissionService;
import org.demo.szml.perssionservice.service.result.PermissionAttributeHandleResult;
import org.demo.szml.perssionservice.service.result.PermissionBaseResult;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Objects;

/**
 * @description: 权限服务实现类
 * @date: 2025/6/16 11:49
 * @version: 1.0
 */
@Service
public class PermissionServiceImpl implements IPermissionService {


    private final IPermissionRepository permissionRepository;

    private final PermissionRoleConfiguration permissionRoleConfiguration;

    public PermissionServiceImpl(IPermissionRepository permissionRepository,
                                 PermissionRoleConfiguration permissionRoleConfiguration){
        this.permissionRepository = permissionRepository;
        this.permissionRoleConfiguration = permissionRoleConfiguration;
    }


    @Override
    public PermissionBaseResult bindDefaultRole(Long userId) {
        //赋值必要属性
        PermissionBaseResult result = PermissionBaseResult.initSuccessResult();
        UserRolesPO userRolesPO = new UserRolesPO();
        userRolesPO.setRoleId(permissionRoleConfiguration.getDefaultRole());
        userRolesPO.setUserId(userId);
        //调用仓储服务持久化绑定结果
        int bindResult = permissionRepository.bindDefaultRole(userRolesPO);
        //返回信息
        if(bindResult == 1){
            return result;
        }
        result.setExtInfo("绑定失败 请稍后再试");
        return result;
    }

    @Override
    public PermissionAttributeHandleResult getAttributeUsers(Long operatorId, Integer page, Integer size) {
        //分页获取操作者权限管理下的用户列表信息
        Page<Long> pageInfo = permissionRepository.getAttributeUsers(operatorId, page, size);
        //这里理应最少返回一条属于自己的数据 如果没有任何数据 则说明身份绑定有问题(虽然不太可能)
        if(Objects.isNull(pageInfo)){
            PermissionAttributeHandleResult handleResult = new PermissionAttributeHandleResult(Collections.emptyList(), 0L);
            handleResult.setHandleSuccess(false);
            handleResult.setExtInfo("没有您对应的角色信息 请反馈");
            return handleResult;
        }
        PermissionAttributeHandleResult handleResult = new PermissionAttributeHandleResult(pageInfo.getRecords(), pageInfo.getTotal());
        handleResult.setHandleSuccess(true);
        return handleResult;
    }

    @Override
    public PermissionAttributeHandleResult getAttributeRelation(Long operatorId, Long userId) {
        //获取用户权限关系
        String attributeResult = permissionRepository.getAttributeRelation(operatorId, userId);
        //如果结果不为空 说明有异常信息
        if(Objects.nonNull(attributeResult)){
            PermissionAttributeHandleResult handleResult = new PermissionAttributeHandleResult(Collections.emptyList(), 0L);
            handleResult.setHandleSuccess(false);
            handleResult.setExtInfo(attributeResult);
        }
        PermissionAttributeHandleResult handleResult = new PermissionAttributeHandleResult(Collections.singletonList(userId), 1L);
        handleResult.setHandleSuccess(true);
        return handleResult;
    }

    @Override
    public PermissionBindingResDTO rebindRole(Long userId, Long operatorId, Integer roleId) {
        //检查操作者和用户权限信息
        PermissionAttributeHandleResult attributeRelation = getAttributeRelation(operatorId, userId);
        //如果权限允许进行角色换绑 则操作
        if(attributeRelation.getHandleSuccess()){
            //如果权限验证没问题 则可以换绑
            int rebind = permissionRepository.rebindRole(operatorId,userId,roleId);
            if(rebind == 1){
                return PermissionBindingResDTO.builder().bindingResult(true).build();
            }
        }

        return PermissionBindingResDTO.builder().extInfo("权限不足").bindingResult(false).build();
    }
}
